﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;

using System.Windows.Forms;

namespace LoginForm
{
    class SunnyIO
    {
        public static List<User> GetUserFromXML(string filename)
        {
            List<User> list = new List<User>();
            using (Stream stream = new FileStream(filename, FileMode.Open))
            {
                XDocument xDoc = XDocument.Load(stream);
                var users = from s in xDoc.Element("users").Elements("user") select s;
                foreach (var u in users)
                {
                    string username = u.Element("username").Value;
                    string password = u.Element("password").Value;
                    User user = new User()
                    {
                        Username = username,
                        Password = password
                    };
                    list.Add(user);
                }
                return list;
            }
        }

        public static void SaveNoteAccount(User u)
        {
            StreamWriter sw = new StreamWriter(FileName.noteaccount);
            if (u != null)
            {
                String name = u.Username;
                String pass = u.Password;
                sw.Write(name + "\r\n");
                sw.Write(pass + "\r\n");
            }
            else
            {
                sw.Write("");
            }
            sw.Close();
        }

        public static User ReadNoteAccount()
        {
            string[] user = File.ReadAllLines(FileName.noteaccount);
            if (user.Length > 0)
            {
                string name = user[0];
                string pass = user[1];
                User u = new User()
                {
                    Username = name,
                    Password = pass
                };
                return u;
            }
            else
            {
                return null;
            }
        }

        public static List<Schedule> GetScheduleFromXML(string filename, string dayofweek)
        {
            List<Schedule> list = new List<Schedule>();
            using (Stream stream = new FileStream(filename, FileMode.Open))
            {
                XDocument xDoc = XDocument.Load(stream);
                var schedules = from schedule in xDoc.Element("schedule").Elements(dayofweek) select schedule;
                foreach (var s in schedules)
                {
                    string id, time, student, education, address;
                    if (s.Attribute("id").Value != null)
                    {
                        id = s.Attribute("id").Value;
                    }
                    else
                    {
                        id = "";
                    }
                    if (s.Element("time").Value != null)
                    {
                        time = s.Element("time").Value;
                    }
                    else
                    {
                        time = "";
                    }
                    if (s.Element("student").Value != null)
                    {
                        student = s.Element("student").Value;
                    }
                    else
                    {
                        student = "";
                    }
                    if (s.Element("education").Value != null)
                    {
                        education = s.Element("education").Value;
                    }
                    else
                    {
                        education = "";
                    }
                    if (s.Element("address").Value != null)
                    {
                        address = s.Element("address").Value;
                    }
                    else
                    {
                        address = "";
                    }
                    Schedule sche = new Schedule()
                    {
                        No = id,
                        Time = time,
                        Student = student,
                        Education = education,
                        Address = address
                    };
                    list.Add(sche);
                }
                stream.Close();
                return list;
            }
        }

        //test
        public static bool InsertSchedule(String filename, List<Schedule> list, String dateofweek)
        {
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc != null)
            {
                if (xDoc.Element("schedule").Elements(dateofweek).Count() > 1)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (xDoc.Element("schedule").Elements(dateofweek).Where(x => x.Attribute("id").Value.Contains(list[i].No)).Count() == 1)
                        {
                            XElement xEle = xDoc.Element("schedule").Elements(dateofweek).Where(x => x.Attribute("id").Value.Contains(list[i].No)).First();
                            Schedule s = list[i];
                            //xEle.Attribute("id").Value = s.No;
                            xEle.Element("time").Value = s.Time;
                            xEle.Element("student").Value = s.Student;
                            xEle.Element("education").Value = s.Education;
                            xEle.Element("address").Value = s.Address;
                        }
                    }
                    xDoc.Save(filename);
                    return true;
                }
            }
            return false;
        }

        public static List<List<Schedule>> GetTimeJobs(String filename)
        {
            List<List<Schedule>> timeJobs = new List<List<Schedule>>();
            List<String> today = new List<string>() { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
            using (Stream stream = new FileStream(filename, FileMode.Open))
            {
                XDocument xDoc = XDocument.Load(stream);
                for (int i = 0; i < today.Count; i++)
                {
                    var schedule = from s in xDoc.Element("schedule").Elements(today[i]) select s;
                    List<Schedule> list = new List<Schedule>();
                    foreach (var t in schedule)
                    {
                        string time = t.Element("time").Value.ToString();
                        string student = t.Element("student").Value.ToString();
                        Schedule sc = new Schedule()
                        {
                            Time = time,
                            Student = student
                        };
                        list.Add(sc);
                    }
                    timeJobs.Add(list);
                }

            }
            return timeJobs;
        }

        public static List<Pupil> GetAllPupils(string filename)
        {
            List<Pupil> list = new List<Pupil>();
            int? id;
            float? money;
            string realname, nickname, phone,education, schedule, school;
            using (Stream stream = new FileStream(filename, FileMode.Open))
            {
                XDocument xDoc = XDocument.Load(stream);
                if (xDoc != null)
                {
                    var pupils = from pupil in xDoc.Element("pupils").Elements("pupil") select pupil;
                    foreach (var p in pupils)
                    {
                        string strId = p.Attribute("id").Value;
                        id = Validation.IsInt(strId);
                        string text1 = p.Element("realname").Value;
                        string text2 = p.Element("nickname").Value;
                        string text3 = p.Element("education").Value;
                        string text4 = p.Element("schedule").Value;
                        string text5 = p.Element("school").Value;
                        string text6 = p.Element("phone").Value;
                        if (!Validation.IsEmpty(text1))
                        {
                            realname = text1;
                        }
                        else
                        {
                            realname = "";
                        }
                        if (!Validation.IsEmpty(text2))
                        {
                            nickname = text2;
                        }
                        else
                        {
                            nickname = "";
                        }
                        if (!Validation.IsEmpty(text3))
                        {
                            education = text3;
                        }
                        else
                        {
                            education = "";
                        }
                        if (!Validation.IsEmpty(text4))
                        {
                            schedule = text4;
                        }
                        else
                        {
                            schedule = "";
                        }
                        if (!Validation.IsEmpty(text5))
                        {
                            school = text5;
                        }
                        else
                        {
                            school = "";
                        }
                        if (!Validation.IsEmpty(text6))
                        {
                            phone = text6;
                        }
                        else
                        {
                            phone = "";
                        }
                        string strMoney = p.Element("money").Value;
                        money = Validation.IsFloat(strMoney);
                        Pupil pup = new Pupil()
                        {
                            Id = id,
                            Realname = realname,
                            Nickname = nickname,
                            Phone = phone,
                            Schedule = schedule,
                            Education = education,
                            School =school,
                            Money = money
                        };
                        list.Add(pup);
                    }
                }
            }
            return list;
        }

        public static void InsertPupil(string filename, List<Pupil> list) {
            XElement xEle = new XElement("pupils",
                    from s in list
                    select new XElement("pupil",
                            new XAttribute("id", s.Id),
                            new XElement("realname", s.Realname),
                            new XElement("nickname",s.Nickname),
                            new XElement("phone", s.Phone),
                            new XElement("schedule", s.Schedule),
                            new XElement("education",s.Education),
                            new XElement("school", s.School),
                            new XElement("money",s.Money)
                        )
                );
            xEle.Save(filename);
        }

        public static bool AddNewPupil(string filename, Pupil p) {
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(p.Id))).Count() == 1)
            {
                return false;
            }
            else {
                xDoc.Element("pupils").Elements("pupil").Last().AddAfterSelf(new XElement("pupil",new XAttribute("id",p.Id),
                                                                                                  new XElement("realname",p.Realname),
                                                                                                  new XElement("nickname",p.Nickname),
                                                                                                  new XElement("phone",p.Phone),
                                                                                                  new XElement("schedule", p.Schedule),
                                                                                                  new XElement("education",p.Education),
                                                                                                  new XElement("school",p.School),
                                                                                                  new XElement("money",p.Money)
                                                                             ));
                xDoc.Save(filename);
                return true;
            }
        }
        public static bool UpdatePupil(string filename, Pupil p) {
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(p.Id))).Count() == 1)
            {
                XElement xEle = xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(p.Id))).First();
                xEle.Element("realname").Value = p.Realname;
                xEle.Element("nickname").Value = p.Nickname;
                xEle.Element("phone").Value = p.Phone;
                xEle.Element("schedule").Value = p.Schedule;
                xEle.Element("education").Value = p.Education;
                xEle.Element("school").Value = p.School;
                xEle.Element("money").Value = p.Money.ToString();
                xDoc.Save(filename);
                return true;
            }
            else {
                return false;
            }
        }
        public static bool DeletePupil(string filename,int id) {
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(id))).Count() == 1)
            {
                xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(id))).Remove();
                xDoc.Save(filename);
                return true;
            }
            else {
                return false;
            }
        }

        public static bool EditPupil(string filename, Pupil p) {
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(p.Id))).Count() == 1)
            {
                XElement xEle = xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(p.Id))).First();
                xEle.Element("realname").Value = p.Realname;
                xEle.Element("nickname").Value = p.Nickname;
                xEle.Element("phone").Value = p.Phone;
                xEle.Element("schedule").Value = p.Schedule;
                xEle.Element("education").Value = p.Education;
                xEle.Element("school").Value = p.School;
                xEle.Element("money").Value = p.Money.ToString();
                xDoc.Save(filename);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static Pupil GetPupilById(string filename, int id){
            XDocument xDoc = XDocument.Load(filename);
            if (xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(id))).Count() == 1)
            {
                XElement xEle = xDoc.Element("pupils").Elements("pupil").Where(x => x.Attribute("id").Value.Contains(Convert.ToString(id))).First();
                string realname = xEle.Element("realname").Value;
                string nickname = xEle.Element("nickname").Value;
                string phone = xEle.Element("phone").Value;
                string schedule = xEle.Element("schedule").Value;
                string education = xEle.Element("education").Value;
                string school = xEle.Element("school").Value;
                string strMoney = xEle.Element("money").Value;
                float money = Convert.ToInt64(strMoney);
                Pupil p = new Pupil()
                {
                    Id = id,
                    Realname = realname,
                    Nickname = nickname,
                    Phone = phone,
                    Schedule = schedule,
                    Education = education,
                    School = school,
                    Money = money
                };
                return p;
            }
            else {
                return null;
            }
        }

    }
}
